//ICC-AVR application builder : 2009/7/29 10:40:45
// Target : M16
// Crystal: 16.000Mhz

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

void port_init(void)
{
 PORTA = 0xFF;
 DDRA  = 0xFF;
 PORTB = 0x01;
 DDRB  = 0x1B;
 PORTC = 0xFF; //m103 output only
 DDRC  = 0xFF;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//TIMER1 initialize - prescale:64
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 250000Hz
// actual value: 250000.000Hz (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1 = 0xFFFF-5000; //setup
 OCR1A = 125+0xFFFF-5000;
 TCCR1A = 0x00;
 TCCR1B = 0x03; //start Timer
}

ISR(TIMER1_COMPA_vect)
{
 //compare occured TCNT1=OCR1A
 PORTA &=~1;
}

ISR(TIMER1_OVF_vect)
{
 //TIMER1 has overflowed
 TCNT1 = 0xFFFF-5000; //reload counter high value
 PORTA |=1;
}

ISR(INT0_vect)
{
 //external interupt on INT0
}

ISR(INT1_vect)
{
 //external interupt on INT1
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 cli(); //disable all interrupts
 port_init();
 timer1_init();

 MCUCR = 0x00;
 GICR  = 0xC0;
 TIMSK = 0x14; //timer interrupt sources
 sei(); //re-enable interrupts
 //all peripherals are now initialized
}



int main()
{
    init_devices();

    while(1);

    return 0;
}

